//
//  SelfDataTool.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/1/8.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "SelfDataTool.h"

@implementation SelfDataTool

+ (void)loginDataInfoWithParam:(NSDictionary *)param success:(void (^)(LoginRes *result))success failure:(void (^)(NSError *error))failure{
    [self postWithUrl:[NSString stringWithFormat:@"%@user/login",BASE_URL] param:param hasT:NO resultClass:[LoginRes class] success:success failure:failure];
}
+(void)getUserInfoWithUserId:(NSString *)userId success:(void (^)(UserInfo *))success failure:(void (^)(NSError *))failure{
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [param setObject:userId forKey:@"orgUserId"];
    [self postWithUrl:[NSString stringWithFormat:@"%@user/info",BASE_URL] param:param hasT:YES resultClass:[UserInfo class] success:success failure:failure];
}
+(void)sendCodeWithParam:(NSDictionary *)param success:(void (^)(JXBaseResponse *))success failure:(void (^)(NSError *))failure{
      [self postWithUrl:[NSString stringWithFormat:@"%@user/registerCode",BASE_URL] param:param hasT:NO resultClass:[JXBaseResponse class] success:success failure:failure];
}
+(void)resetPwdWithParam:(NSDictionary *)param success:(void (^)(JXBaseResponse *))success failure:(void (^)(NSError *))failure{
    [self postWithUrl:[NSString stringWithFormat:@"%@user/resetPassword",BASE_URL] param:param hasT:NO resultClass:[JXBaseResponse class] success:success failure:failure];
}
+(void)registWithParam:(NSDictionary *)param success:(void (^)(JXBaseResponse *))success failure:(void (^)(NSError *))failure{
    [self postWithUrl:[NSString stringWithFormat:@"%@user/register",BASE_URL] param:param hasT:NO resultClass:[JXBaseResponse class] success:success failure:failure];
}

+ (void)getOrderListWithOrderType:(NSString *)type success:(void (^)(OrderList *result))success failure:(void (^)(NSError *error))failure{
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [param setObject:type forKey:@"orderType"];
    [self postWithUrl:[NSString stringWithFormat:@"%@order/list",BASE_URL] param:param hasT:YES resultClass:[OrderList class] success:success failure:failure];
}

+ (void)getAddressListSuccess:(void (^)(AddressDetailList *result))success failure:(void (^)(NSError *error))failure{
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [self postWithUrl:[NSString stringWithFormat:@"%@user/getAdresList",BASE_URL] param:param hasT:YES resultClass:[AddressDetailList class] success:success failure:failure];
}

+ (void)deleteAddressWithID:(NSString *)adresId success:(void (^)(JXBaseResponse *))success failure:(void (^)(NSError *))failure{
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [param setObject:adresId forKey:@"addressId"];
    [self postWithUrl:[NSString stringWithFormat:@"%@user/addressDel",BASE_URL] param:param hasT:YES resultClass:[OrderList class] success:success failure:failure];
}


+ (void)addAddressWithAddressee:(NSString *)addressee phoneCode:(NSString *)phoneCode provinceId:(NSString *)provinceId cityId:(NSString *)cityId areaId:(NSString *)areaId detailAdres:(NSString *)detailAdres addressId:(NSString *)addressId success:(void (^)(JXBaseResponse *result))success failure:(void (^)(NSError *error))failure{
    NSString *url = @"user/addressAdd";
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [param setObject:addressee forKey:@"addressee"];
    [param setObject:phoneCode forKey:@"phoneCode"];
    [param setObject:provinceId forKey:@"provinceId"];
    [param setObject:cityId forKey:@"cityId"];
    [param setObject:areaId forKey:@"areaId"];
    [param setObject:detailAdres forKey:@"detailAdres"];
    if (addressId) {
        [param setObject:addressId forKey:@"addressId"];
        url = @"user/addressUpdate";
    }
    [self postWithUrl:[NSString stringWithFormat:@"%@%@",BASE_URL,url] param:param hasT:YES resultClass:[OrderList class] success:success failure:failure];
}

+(void)getOrderDetailAddress:(NSString *)orderCode success:(void (^)(AddressDetail *result))success failure:(void (^)(NSError *error))failure{
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [param setObject:orderCode forKey:@"orderCode"];
    [self postWithUrl:[NSString stringWithFormat:@"%@order/detail",BASE_URL] param:param hasT:YES resultClass:[AddressDetail class] success:success failure:failure];
}

+(void)cancelOrder:(NSString *)orderCode success:(void (^)(JXBaseResponse *result))success failure:(void (^)(NSError *error))failure{
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [param setObject:orderCode forKey:@"orderCode"];
    [self postWithUrl:[NSString stringWithFormat:@"%@order/cancel",BASE_URL] param:param hasT:YES resultClass:[JXBaseResponse class] success:success failure:failure];
}

+(void)commitFeedback:(NSString *)content contact:(NSString *)contact success:(void (^)(JXBaseResponse *result))success failure:(void (^)(NSError *error))failure{
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [param setObject:content forKey:@"content"];
    [param setObject:contact forKey:@"contact"];
    [self postWithUrl:[NSString stringWithFormat:@"%@user/feedback",BASE_URL] param:param hasT:YES resultClass:[JXBaseResponse class] success:success failure:failure];
}

+(void)getUserOtherInfo:(void (^)(MyPageInfo *result))success failure:(void (^)(NSError *error))failure{
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [self postWithUrl:[NSString stringWithFormat:@"%@user/myPage",BASE_URL] param:param hasT:YES resultClass:[MyPageInfo class] success:success failure:failure];
}

+(void)getCollectList:(void (^)(ProductSearch *result))success failure:(void (^)(NSError *error))failure{
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [self postWithUrl:[NSString stringWithFormat:@"%@product/mymark",BASE_URL] param:param hasT:YES resultClass:[ProductSearch class] success:success failure:failure];
}

+(void)getRecommendAppList:(void (^)(RemndAppList *result))success failure:(void (^)(NSError *error))failure{
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [self postWithUrl:[NSString stringWithFormat:@"%@app/push",BASE_URL] param:param hasT:NO resultClass:[RemndAppList class] success:success failure:failure];
}


+(void)setUserInfo:(NSString *)userName userGender:(NSString *)userGender userImage:(UIImage *)uesrImage success:(void (^)(JXBaseResponse *result))success failure:(void (^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@user/upload",BASE_URL];
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [param setObject:userName forKey:@"nickName"];
    [param setObject:userGender forKey:@"userGender"];
    
    NSDictionary *params = [SignUtils getParams:param shouldLogin:YES];
    
    // 1.获得请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    // 2.发送POST请求
    [mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (uesrImage!=nil) {
            NSData *data = UIImageJPEGRepresentation(uesrImage, 1.0);
            // 拼接文件参数
            [formData appendPartWithFileData:data name:@"file" fileName:@"status.jpg" mimeType:@"image/jpeg"];
        }
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success([[JXBaseResponse class] objectWithJSONData:responseObject]);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
}

+(void)oauthLoginWithParam:(NSDictionary *)param success:(void (^)(LoginRes *))success failure:(void (^)(NSError *))failure
{
    [self postWithUrl:[NSString stringWithFormat:@"%@user/oauth",BASE_URL] param:param hasT:NO resultClass:[LoginRes class] success:success failure:failure];
}

+(void)getVolumeList:(NSDictionary *)param success:(void (^)(VoucherList *))success failure:(void (^)(NSError *))failure{
    [self postWithUrl:[NSString stringWithFormat:@"%@user/voucherList",BASE_URL] param:param hasT:YES resultClass:[VoucherList class] success:success failure:failure];
}

+(void)getShareOrderList:(NSDictionary *)param success:(void (^)(ShareProductRes *))success failure:(void (^)(NSError *))failure{
    [self postWithUrl:[NSString stringWithFormat:@"%@forum/sunSignleProduct",BASE_URL] param:param hasT:YES resultClass:[ShareProductRes class] success:success failure:failure];
}

+(void)validateVoucher:(NSDictionary *)param success:(void (^)(CheckVoucherRes *))success failure:(void (^)(NSError *))failure{
    [self postWithUrl:[NSString stringWithFormat:@"%@user/validateVoucher",BASE_URL] param:param hasT:YES resultClass:[CheckVoucherRes class] success:success failure:failure];
}
@end
